package com.dao.impl;

import com.bean.Customer;
import com.bean.Goods;
import com.bean.Stock;
import com.dao.StockDao;
import com.util.JDBC_Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StockDaoImpl implements StockDao {
    Connection conn = null;
    ResultSet rs= null;
    PreparedStatement ps = null;

    @Override
    public List<Stock> showStocks() {
        List<Stock> list = new ArrayList<>();
        conn = JDBC_Util.getConnection();

        String sql = "select t_stock.id, goods_name, manufactor, sell_price,type ,number from t_stock" +
                " inner join t_goods tg on t_stock.goods_id = tg.id where number > 0";

        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String goodsName = rs.getString("goods_name");
                String manufactor = rs.getString("manufactor");
                String type = rs.getString("type");
                int number = rs.getInt("number");
                int sellPrice = rs.getInt("sell_price");

                Goods goods = new Goods();
                goods.setSellPrice(sellPrice);
                goods.setType(type);
                goods.setManufactor(manufactor);
                goods.setName(goodsName);
                Stock stock = new Stock();
                stock.setGoods(goods);
                stock.setNumber(number);
                stock.setId(id);

                list.add(stock);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return list;
    }

    @Override
    public Stock showStock(int key) {
        conn = JDBC_Util.getConnection();
        Stock stock = new Stock();
        String sql = "select goods_name, manufactor, type ,sell_price,buy_price,number from t_stock" +
                " inner join t_goods tg on t_stock.goods_id = tg.id" +
                " where t_stock.id = ? ";

        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,key);
            rs = ps.executeQuery();
            while (rs.next()) {
                String goodsName = rs.getString("goods_name");
                String manufactor = rs.getString("manufactor");
                String type = rs.getString("type");
                int number = rs.getInt("number");
                int sellPrice = rs.getInt("sell_price");
                int buyPrice = rs.getInt("buy_price");

                Goods goods = new Goods();
                goods.setType(type);
                goods.setManufactor(manufactor);
                goods.setName(goodsName);
                goods.setSellPrice(sellPrice);
                goods.setBuyPrice(buyPrice);


                stock.setGoods(goods);
                stock.setNumber(number);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return stock;
    }

    @Override
    public List<Stock> findStock(String startNumb, String endNumb, String type, String name) {
        List<Stock> list = new ArrayList<>();
        conn = JDBC_Util.getConnection();

        String sql = "select t_stock.id, goods_name, manufactor, type ,number from t_stock" +
                " inner join t_goods tg on t_stock.goods_id = tg.id where 1 = 1";

        System.out.println(startNumb);
        if(!startNumb.equals("") && startNumb != null){
            sql += " and sell_price > "+Integer.parseInt(startNumb)+"";
        }

        if(!endNumb.equals("") && endNumb != null){
            sql += " and buy_price < "+Integer.parseInt(endNumb)+"";
        }

        if(!type.equals("") && type != null){
            sql += " and type like '%"+type+"%'";
        }

        if(!name.equals("") && name != null){
            sql += " and goods_name like '%"+name+"%'";
        }

        try {
            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery();

            while (rs.next()){
                int id = rs.getInt("id");
                String goodsName = rs.getString("goods_name");
                String manufactor = rs.getString("manufactor");
                String newType = rs.getString("type");
                int number = rs.getInt("number");

                Goods goods = new Goods();
                goods.setType(newType);
                goods.setManufactor(manufactor);
                goods.setName(goodsName);
                Stock stock = new Stock();
                stock.setGoods(goods);
                stock.setNumber(number);
                stock.setId(id);

                list.add(stock);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return list;
    }

    @Override
    public int updateSellPrice(int key,int sellPrice) {
        String sql = "update t_goods set sell_price = "+sellPrice+"" +
                " inner join t_stock ts on t_goods.id = ts.goods_id" +
                " where ts.id = "+key+"";
        int m = JDBC_Util.update(sql);
        return m;
    }
}
